package com.htu.courseService.courseContents.controller;

import cn.hutool.core.lang.tree.Tree;
import com.htu.courseService.common.vo.R;
import com.htu.courseService.courseContents.service.DirectoriesService;
import com.htu.courseService.courseContents.vo.DirectoriesDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courseDirectories")
@RequiredArgsConstructor
@Slf4j
public class DirectoriesController {
    private final DirectoriesService directoriesService;

    /**
     * 创建目录
     * @param directoriesDto
     * @return
     */
    @PostMapping("/create")
    public R<?> create(@RequestBody DirectoriesDto directoriesDto){
        directoriesService.saveDirectories(directoriesDto);
        return R.success("新建目录成功");
    }

    /**
     * 查看目录列表
     * @param courseId
     * @return
     */
    @GetMapping("/tree")
    public R<List<Tree<Integer>>> list(Integer courseId){
        List<Tree<Integer>> directories = directoriesService.getDirectoriesList(courseId);
        return R.success(directories);
    }

    @DeleteMapping("/delete")
    public R<?> delete(Integer directoriesId){
        directoriesService.deleteDirectories(directoriesId);
        return R.success("删除成功");
    }

    @PutMapping("/update")
    public R<?> update(@RequestBody DirectoriesDto directoriesDto){
        directoriesService.updateDirectoriesName(directoriesDto);
        return R.success("修改成功");
    }
}
